<template>
  <view>
    <view class="search-box">
      <my-search @click="gotoSearch"></my-search>
    </view>

    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular="ture">
      <swiper-item v-for="(item, i) in swiperList" :key="i">
        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <view class="nav-list">
      <view class="nav-item" v-for="(item, i ) in navList" :key="i" @click="navClickHandler(item)">
        <image class="nav-img" :src="item.image_src"></image>
      </view>
    </view>
    <view class="floor-list">
      <view class="floor-item" v-for="(item, i) in floorList" :key="i">
        <!-- 标题 -->
        <image class="floor-title" :src="item.floor_title.image_src"></image>
        <!-- 图片 -->
        <view class="floor-img-box">
          <navigator class="left-img-box" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width+'rpx'}"
              mode="widthFix"></image>
          </navigator>
          <view class="right-img-box">
            <navigator class="right-img-item" v-for="(item2, i2) in item.product_list" :key="i2" v-if="i2!==0"
              :url="item2.url">
              <image :src="item2.image_src" :style="{width:item2.image_width+'rpx'}" mode="widthFix"></image>
            </navigator>
          </view>

        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import {
    getSwiperList,
    getNavList,
    getFloorList
  } from '@/utils/api.js'
  import
  badgeMix
  from '@/mixins/tabbar-badge.js'
  export default {
    mixins: [badgeMix],
    data() {
      return {
        // 轮播图数据列表
        swiperList: [],
        navList: [],
        floorList: []
      };
    },
    // 页面生命周期
    onLoad() {
      this.getSwiperListEx()
      this.getNavListEx()
      this.getFloorListEx()
    },
    methods: {
      // 请求轮播图的数据
      async getSwiperListEx() {
        const {
          data: res
        } = await getSwiperList()
        if (res.meta.status !== 200) {
          this.$showMsg()
          return
        }
        this.swiperList = res.message
      },
      //  获取导航
      async getNavListEx() {
        const {
          data: res
        } = await getNavList()
        if (res.meta.status !== 200) {
          this.$showMsg()
          return
        }
        this.navList = res.message
      },
      // 获取楼层
      async getFloorListEx() {
        const {
          data: res
        } = await getFloorList()
        if (res.meta.status !== 200) {
          this.$showMsg()
          return
        }
        res.message.forEach((i) => {
          i.product_list.forEach((j) => {
            const queryParam = j.navigator_url.split('?')[1]
            j.url = '/subpkg/goods_list/goods_list?' + queryParam
          })
        })
        this.floorList = res.message
      },
      navClickHandler(e) {
        if (e.name === '分类') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      gotoSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 330rpx;

    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }

  .nav-list {
    display: flex;
    justify-content: space-around;
    margin: 15rpx 0;

    .nav-img {
      width: 128rpx;
      height: 140rpx;
    }
  }

  .floor-title {
    height: 60rpx;
    width: 100%;
  }

  .right-img-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }

  .floor-img-box {
    display: flex;
    padding: 10rpx;
  }

  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }
</style>